$((function() {
	var currentUser = AV.User.current();
	var username = currentUser._serverData.username;
	var query = new AV.Query('gouwuche');
	query.equalTo('username', username);
	query.find().then(function(results1) {
		$("#gouwuche").html("");
		var index = 0;
		var arrid = [];
		var nums = [];
		var length = results1.length;
		for(var i = 0; i < length; i++) {
			var que = new AV.Query('Product');
			que.equalTo('id', results1[i].attributes.id);
			arrid.push(results1[i].id);
			nums.push(results1[i].attributes.num);
			que.find().then(function(results) {
				var src = results[0].attributes.image.split(";");
				var price = results[0].attributes.price.split("¥");
				var str = '<div class="shop_nav" id="' + arrid[index] + '"><span><input type="checkbox" /><img src="' + src[0] + '"/></span>';
				str += '<span><p>' + results[0].attributes.title + '</p></span><span class="oneprice">' + price[1] + '</span>';
				str += '<span><i class="Hui-iconfont del">&#xe6a1;</i><input type="text" size="1" class="number" value="' + nums[index] + '" />';
				str += '<i class="Hui-iconfont add">&#xe600;</i><label class="tishi">请至少选择一件</label></span>';
				str += '<span class="fb price">' + price[1] * parseInt(nums[index]) + '</span><span class="removeshop">删除</span></div>';
				$("#gouwuche").append(str);
				index++;
				if(index == length) {
					$(".removeshop").click(function() { //删除单件物品
						$(this.parentNode).remove();
						var todo = AV.Object.createWithoutData('gouwuche', $(this.parentNode).attr("id"));
						todo.destroy().then(function(success) {
							// 删除成功
						}, function(error) {
							// 删除失败
						});
						count();
					}).mouseover(function() {
						$(this).css("cursor", "pointer");
					});
					$("#gouwuche input[type='checkbox']").click(function() {
//						if(this.checked) {
//							$(this).attr("checked", "checked");
//						} else {
//							$(this).removeAttr("checked");
//						}
						count();
					});
					//数量减
					$("#gouwuche .shop_nav i.del").click(function() {
						var $num = parseInt($(this).next().val());
						if(!isNaN($num)) {
							if($num == 1) {
								this.parentNode.children[3].innerHTML = "请至少选择一件";
								this.parentNode.children[3].style.display = "block";
								return;
							}
							$num--;
							$(this).next().val($num);
							this.parentNode.children[3].style.display = "none";
							jisuan(this, $num);
							var num1 = $num.toString();
							var todo = AV.Object.createWithoutData('gouwuche', $(this.parentNode.parentNode).attr("id"));
							// 修改属性
							todo.set('num', num1);
							// 保存到云端
							todo.save();
						} else {
							this.parentNode.children[3].innerHTML = "请输入数字";
							this.parentNode.children[3].style.display = "block";
							$(this).next().val("1");
							jisuan(this, 1);
						}
						count();
					});
					//数量加
					$("#gouwuche .shop_nav i.add").click(function() {
						var $num = parseInt(this.parentNode.children[1].value);
						//				console.log($num)
						if(!isNaN($num)) {
							if($num >= 1) {
								$num++;
								this.parentNode.children[1].value = $num;
								this.parentNode.children[3].style.display = "none";
								jisuan(this, $num);
								var num1 = $num.toString();
								var todo = AV.Object.createWithoutData('gouwuche', $(this.parentNode.parentNode).attr("id"));
								// 修改属性
								todo.set('num', num1);
								// 保存到云端
								todo.save();
							}
						} else {
							this.parentNode.children[3].innerHTML = "请输入数字";
							this.parentNode.children[3].style.display = "block";
							this.parentNode.children[1].value = "1";
							jisuan(this, 1);
						}
						count();
					});
				}
			}, function(error) {});
		}

	}, function(error) {});

	$(".quanxuan").click(function() {
		var ischecked = this.checked
		$("#gouwuche input[type='checkbox']").each(function(i) {
			$("#gouwuche input[type='checkbox']")[i].checked = ischecked
		});
//		console.log($("#gouwuche input[type='checkbox']")[1].checked);
		console.log(ischecked)
		count();
	});

	//	$(".quanxuan").click(function() {
	//		if(this.checked) {
	//			console.log(111)
	//			$("#gouwuche input[type='checkbox']").attr("checked", true);
	//			$(".quanxuan").attr("checked", true);
	//		} else {
	//			$("#gouwuche input[type='checkbox']").removeAttr("checked");
	//			$(".quanxuan").removeAttr("checked");
	//		}
	//		count();
	//	});

	function count() {
		var price = 0;
		var con=0;
		$("#gouwuche input[type='checkbox']").each(function(i) {
			if($("#gouwuche input[type='checkbox']")[i].checked==true){
				price += parseFloat(this.parentNode.parentNode.children[4].innerHTML);
				con+=1;
			}
		});
		$(".shop_bott p span:nth-child(1)").html(con);
		price = price.toString();
			var star = price.indexOf(".");
			var xiaoshu;
			if(star != -1) {
				xiaoshu = price.substr(star + 1, 2);
				if(xiaoshu.length == 1) {
					xiaoshu = xiaoshu + "0";
				}
			} else {
				xiaoshu = "00";
			}
			//			console.log(price)
			price = parseInt(price)
			//			console.log(price)
			var newprice = price + "." + xiaoshu;
			$(".shop_bott p span:nth-child(2)").html(newprice);
		} 
//		else {
//			$(".shop_bott p span:nth-child(2)").html("0.00");
//		}

//		//		console.log($("#gouwuche input[type='checkbox']"))
//		var checked = $("#gouwuche input[checked='checked']");
//		//console.log(checked.length);
//		$(".shop_bott p span:nth-child(1)").html(checked.length);
//		if(checked.length > 0) {
//			//var div = $("#gouwuche div");
//			var price = 0;
//			for(var i = 0; i < checked.length; i++) {
//				price += parseFloat(checked[i].parentNode.parentNode.children[4].innerHTML);
//			}
//			price = price.toString();
//			var star = price.indexOf(".");
//			var xiaoshu;
//			if(star != -1) {
//				xiaoshu = price.substr(star + 1, 2);
//				if(xiaoshu.length == 1) {
//					xiaoshu = xiaoshu + "0";
//				}
//			} else {
//				xiaoshu = "00";
//			}
//			//			console.log(price)
//			price = parseInt(price)
//			//			console.log(price)
//			var newprice = price + "." + xiaoshu;
//			$(".shop_bott p span:nth-child(2)").html(newprice);
//		} else {
//			$(".shop_bott p span:nth-child(2)").html("0.00");
//		}
//	}
	count();
	$("#jiesuan").click(function() {
		var checked = $("#gouwuche input[checked='checked']");
		//console.log(checked.length);
		for(var i = 0; i < checked.length; i++) {
			var todo = AV.Object.createWithoutData('gouwuche', checked[i].parentNode.parentNode.id);
			todo.destroy().then(function(success) {
				alert("结算成功");
				location.href = "gouwuche.html"
			}, function(error) {
				// 删除失败
			});
		}
	});

	function jisuan(t, n) { //计算单行总价
		var jisuan = parseFloat(t.parentNode.parentNode.children[2].innerHTML);
		jisuan *= n;
		jisuan = jisuan.toString();
		var star = jisuan.indexOf(".");
		if(star != -1) {
			xiaoshu = jisuan.substr(star + 1, 2);
			if(xiaoshu.length == 1) {
				xiaoshu = xiaoshu + "0";
			}
		} else {
			xiaoshu = "00";
		}
		jisuan = parseInt(jisuan)
		$(t.parentNode).next().html(jisuan + "." + xiaoshu);
	}

})());